package com.appsky.drift.adapter;

import com.alibaba.fastjson.JSONObject;
import com.appsky.drift.R;
import com.appsky.drift.activity.ChatMainActivity;
import com.appsky.drift.activity.ChatTraditionActivity;
import com.appsky.drift.api.AppSkyApp;
import com.appsky.drift.api.DLog;
import com.appsky.drift.api.JsonFactory;
import com.appsky.drift.api.UserInfo;
import com.appsky.drift.api.Utils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class NearbyPersonListItem extends RelativeLayout {

	private static final int FEMALE_SEX = 0;

	private static final int MALE_SEX = 1;

	private static final int SECRECY_SEX = 2;

	private Context context;

	private TextView personNick;

	private ImageView sexIcon;

	private TextView stateText;

	private Button joinRoom;
	
	private AppSkyApp app;

	public NearbyPersonListItem(Context context) {
		super(context);
		this.context = context;
		app = (AppSkyApp) context.getApplicationContext();
		initView();
	}

	public void show(UserInfo item) {
		personNick.setText(item.getNick());
		setSexIcon(item);
		setStateIcon(item);
	}

	private void initView() {
		RelativeLayout view = (RelativeLayout) View.inflate(context,
				R.layout.nearby_person_list_item, null);
		personNick = (TextView) view.findViewById(R.id.nick_text);
		sexIcon = (ImageView) view.findViewById(R.id.sex_image);
		stateText = (TextView) view.findViewById(R.id.state_text);
		joinRoom = (Button) view.findViewById(R.id.join_room_button);
		addView(view);
	}

	private void setSexIcon(UserInfo item) {
		switch (item.getSex()) {
		case FEMALE_SEX:
			sexIcon.setImageResource(R.drawable.female);
			break;
		case MALE_SEX:
			sexIcon.setImageResource(R.drawable.male);
			break;
		case SECRECY_SEX:
			sexIcon.setImageResource(R.drawable.secrecy);
			break;

		}
	}

	private void setStateIcon(final UserInfo item) {
		if (item.isDrifting()) {// 飘着
			stateText.setText("飘");
			stateText.setTextColor(Color.BLUE);
			joinRoom.setVisibility(View.INVISIBLE);
		} else if (item.isRoomOwner()) {// 房主
			stateText.setText("主");
			stateText.setTextColor(Color.RED);
			joinRoom.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					JsonFactory jsonFactory = JsonFactory.getFactory();
					JSONObject json = jsonFactory.makeJoinRoomJson(
							app.myInfo.getUserId(), item.getUserId());
					Intent intent = new Intent(Utils.ACTION_SEND_MESSAGE);
					intent.putExtra("json", json.toJSONString());
					context.sendBroadcast(intent);
					app.myInfo.setRoomId(item.getUserId());
					Intent intentToChat = new Intent(context,
							ChatMainActivity.class);
					context.startActivity(intentToChat);
				}
			});
			joinRoom.setVisibility(View.VISIBLE);
		} else {// 已加入其他房间
			stateText.setText("聊");
			stateText.setTextColor(Color.GREEN);
			joinRoom.setVisibility(View.INVISIBLE);
		}
	}
}
